package mobi.weq.dao;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import mobi.weq.model.SensorObservation;
import mobi.weq.util.Util;
import mobi.weq.util.edinburgh.EdinburghMap;
import mobi.weq.util.map.Bounds;

public class PollutionData {
	private static HashMap<String, SensorObservation> currentDataCache = new HashMap<String, SensorObservation>();
	private static Date lastUpdateCache;

	// public static ArrayList<SensorObservation>
	// getDummySensorObservationLastNDays(int days, double gridLat, double
	// gridLng) {
	// return EdinburghDataGenerator.generateDataForAGridNDays(days, new Date(),
	// 0, 0);
	// }
	//
	// public static SensorObservation getDummySensorObservation() {
	// return EdinburghDataGenerator.getOneDummyObservation();
	// }
	//
	// /*******************************************/
	public static SensorObservation getCurrentDataForPosition(double lat, double lng) throws SQLException {
		Date d = new Date();
		if (lastUpdateCache == null || lastUpdateCache.getTime() / 3600000 != d.getTime() / 3600000) {
			currentDataCache.clear();
			lastUpdateCache = d ;
		}

		if (!Util.coordsLieInEdinburgh(lat, lng)) {
			lat = EdinburghMap.CENTER_LAT;
			lng = EdinburghMap.CENTER_LNG;
		}

		String key = Util.getGridId(lat, lng);
		if (!currentDataCache.containsKey(key)) {
			System.out.println("not using cache");
			SensorObservationDao dao = new SensorObservationDao();
			List<SensorObservation> list = dao.getLatestObservationForThePosition(lat, lng);
			currentDataCache.put(key, list.get(0));
		}

		return currentDataCache.get(key);
	}

	public static List<SensorObservation> getCurrentDataForPositionLastNDays(int days, double lat, double lng) throws SQLException {
		if (!Util.coordsLieInEdinburgh(lat, lng)) {
			lat = EdinburghMap.CENTER_LAT;
			lng = EdinburghMap.CENTER_LNG;
		}

		SensorObservationDao dao = new SensorObservationDao();
		List<SensorObservation> list = dao.getLatestObservationForThePosition(days, lat, lng);
		return list;
	}

	public static List<SensorObservation> getCurrentGridsAndObservationForPosition(Bounds bounds, Date d) throws SQLException {
		SensorObservationDao dao = new SensorObservationDao();
		List<SensorObservation> list = dao.getGridsWithLatestObservationInTheBounds(bounds.southWest.latitude, bounds.southWest.longitude,
				bounds.northEast.latitude, bounds.northEast.longitude, d.getHours());
		return list;
	}

	public static void main(String[] args) {
		try {
			// getCurrentDataForPosition(0, 0);//
			getCurrentGridsAndObservationForPosition(null, new Date());
			// select * from so where latitude >= 55.904523310371175 and
			// latitude <= 55.91652331037118 and longitude >=
			// -3.4920418031250047 and longitude <= -3.4760418031250043 order by
			// timestamp desc limit 1

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
